package com.hermes.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.hermes.biz.common.model.user.UserPublicDO;
import com.hermes.biz.common.util.string.StringUtil;
/**
 * 用户登录等一些session处理工具类
 * @author 小桔子猫
 *
 */
public class LoginHelperUtil {

	private Logger log = Logger.getLogger(LoginHelperUtil.class);
	
	private String username;
	
	private String userId;
	
	HttpServletRequest request;
	
	public LoginHelperUtil(HttpServletRequest request){
		this.request=request;
	}
	
	public  boolean hasLogined(){	
		HttpSession session = request.getSession();
		try{
			username = (String) session.getAttribute("userName");			
			if(StringUtil.isNotBlank(username)){
				return true;
			}
			return false;
		}catch(Exception e){
			log.info(e.toString()+userId);
			return false;
		}
	}
	
	public String getCurrentUserName(){
		HttpSession session = request.getSession();
		try{
			username = (String) session.getAttribute("userName");			
			if(StringUtil.isNotBlank(username)){
				return username;
			}
		}catch(Exception e){
			log.info(e.toString());
			return "";
		}
		return "";
		
	}
	
	public UserPublicDO getUserPublicDO(){
		
		HttpSession session = request.getSession();
		try{
			UserPublicDO userPublicDO = (UserPublicDO) session.getAttribute("userPublicDO");			
			if(userPublicDO!=null){
				log.error("__---___--___--__");
				return userPublicDO;
			}
		}catch(Exception e){
			log.error(e.toString());
			log.error("__---___--___--__+++");
			return new UserPublicDO();
		}
		return new UserPublicDO();
	}
	
	public boolean quitLogin(String username){
		HttpSession session = request.getSession();
		try{
			username = (String) session.getAttribute("userName");			
			if(StringUtil.isNotBlank(username)){
				session.removeAttribute(username);
				return true;
			}
		}catch(Exception e){
			log.info(e.toString()+username);
			return false;
		}
		return false;
	}
	
	public boolean quitLogin(){
		HttpSession session = request.getSession();
		try{
			session.removeAttribute("userName");
			return true;
		}catch(Exception e){
			log.info(e.toString());
			return false;
		}
	}
}
